package com.my.screenadapter.lifecircle;


import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.my.screenadapter.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class BlankFragment extends Fragment {
    private static final String TAG = "BlankFragment---";
    private static final String KEY_INDEX = "index";
    private int mCurrentIndex;

    public BlankFragment() {
        // Required empty public constructor
    }

    public static BlankFragment newInstance(String one, String two) {
        return new BlankFragment();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.i(TAG, "onConfigurationChanged: ");
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState: ");
        outState.putInt(KEY_INDEX, mCurrentIndex);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(TAG, "onActivityCreated: ");
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.i(TAG, "onAttach: ");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 对比上述看电子书的情形，假如电子书也是由fragment构建，采用setRetainInstance(true)的方式，也可以保证在旋转时当前页数同fragment一起被保留下来。
         但是，被保留的fragment有可能会因系统回收内存而被销毁，那么页数也就丢失了。
         但是采用 onSaveInstanceState(Bundle outState) 的优势在于，bundle对象会保存的久一些（具体多久不清楚 TODO）。
         如果Fragment只是简单的UI View，像是TextView, Button, CheckBox, ImageView...
         不建议使用setRetainInstance方法，只需要记住当前fragment的index，然后在设备旋转后根据数据重新实例化一个fragment。
         因为不包含大量数据，旋转的过程中几乎可以用“无缝切换”来形容。
         */
//        setRetainInstance(true);
        if (savedInstanceState != null) {
            Log.i(TAG, "onCreate: savedInstanceState    before==="+mCurrentIndex);
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
            Log.i(TAG, "onCreate: savedInstanceState    after==="+mCurrentIndex);
        }else {
            Log.i(TAG, "onCreate: savedInstanceState==null");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.i(TAG, "onCreateView: ");
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i(TAG, "onViewCreated: ");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: mCurrentIndex===" + mCurrentIndex);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
        mCurrentIndex = 1111;
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: ");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(TAG, "onDestroyView: ");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    }
}
